/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：User_amount_log接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： User_amount_logRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Transactions;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class User_amount_logRepository:BaseRepository<User_amount_log,Int32>, IUser_amount_logRepository
    {
        public User_amount_logRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public int Add(User_amount_log model)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                
                    int id = (int)_dbConnection.Insert(model);
                    var user = _dbConnection.Get<Models.Users>(model.user_id);
                    user.amount += model.value;
                    _dbConnection.Update(user);
                    scope.Complete();
                    return id;
                }
            }
        }
        public int Add(User_amount_log model,int leixing)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                
                    int id = (int)_dbConnection.Insert(model);
                    var user = _dbConnection.Get<Models.Users>(model.user_id);
                    if (leixing == 1)
                        user.cash_point += model.value;
                    if (leixing == 2)
                        user.shop_point += model.value;
                    _dbConnection.Update(user);
                    scope.Complete();
                    return id;
                }
            }
        }
    }
}